app.controller('MoneyCtrl', ['$scope', '$timeout', '$http', '$compile', 'R', 'toaster', '$modal', '$rootScope', '$location', function($scope, $timeout, $http, $compile, R, toaster, $modal, $rootScope, $location) {
    $scope.currentpage = 20;
    $scope.totalCount = 0;

    $scope.query = {
        keyword: "",
        pageNo: 0,
        state: -1,
        keywordType: 0,
        pageSize: $scope.currentpage
    }

    $scope.opened = [];
    $scope.open = function($event,index) {
        $event.preventDefault();
        $event.stopPropagation();
        $scope.opened[index] = true;
    };

    $scope.mappers = {
        state: [{
            'key': -1,
            'value': '所有状态'
        },{
            'key': 0,
            'value': '待支付'
        },{
            'key': 1,
            'value': '支付成功'
        },{
            'key': 2,
            'value': '支付失败'
        },{
            'key': 3,
            'value': '申请退款'
        },{
            'key': 4,
            'value': '退款成功'
        },{
            'key': 5,
            'value': '退款失败'
        }],
        keywordType: [{
            'key': 0,
            'value': '订单号'
        },{
            'key': 1,
            'value': '手机号'
        }]
    };

    $scope.submit = function(){
        $scope.query.pageNo = 0;
        $scope.getItems();
    }

    $scope.updateDJ = function(item){
        $rootScope.dialog("src/money/tpl/modal.html", "updateDJMoneyCtrl", {
            Item: function () {
                return item;
            }
        }, {'size': 'sm'})
        .result
        .then(function (res) {
            $scope.getItems();
        })
    }

    $scope.del = function(item){
        $rootScope.confirm('确认给用户退款'+item.price+'元?', '提示信息')
        .then(function () {
            R.refundOrder({
                ids: item.id
            }).then(function(data){
                $("body").append(data.bizData.sHtmlText);
                toaster.success('操作成功');
                $scope.getItems();
            });
        });
    }

    $scope.rowCollectionPage = [];

    $scope.getItems = function(tableState, ctrl) {
        if (!$scope.stCtrl && ctrl) {
            $scope.stCtrl = ctrl;
        }

        if (!tableState && $scope.stCtrl) {
            $scope.stCtrl.pipe();
            return;
        }

        var pagination = tableState.pagination;

        $scope.query.pageNo = Math.ceil(pagination.start / $scope.currentpage);

        R.getDepositByCondition($scope.query).then(function(data) {
            $scope.rowCollectionPage = data.bizData.list;
            $scope.totalCount = data.bizData.count;
            $scope.pages = tableState.pagination.numberOfPages = Math.ceil(data.bizData.count / $scope.currentpage);
        });
    };
}]);

app.controller("updateDJMoneyCtrl", ["$rootScope", "$scope", "$state", "$stateParams", '$modalInstance', 'Item', 'R', 'toaster', function ($rootScope, $scope, $state, $stateParams, $modalInstance, Item, R, toaster) {
    R.getDepositPercent().then(function(data){
        $scope.percent = data.bizData.percent;
    });
    $scope.ok = function(){
        if (!$scope.percent || $scope.percent < 0){
            return;
        }

        $rootScope.confirm('确认将订单比例更改为'+$scope.percent+'%?', '提示信息')
        .then(function () {
            R.updateDepositPercent({percent: $scope.percent}).then(function(){
                toaster.success('修改成功');
                $modalInstance.dismiss('cancel');
            });
        });
    }
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
}]);